package com.flexandair.view
{
	import caurina.transitions.Tweener;
	
	import com.flexandair.ApplicationFacade;
	import com.flexandair.view.components.ClassProp;
	import com.flexandair.view.components.ObjectCanvas;
	import com.flexandair.view.components.ObjectDesignerCanvas;
	
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ObjectDesignerCanvasMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ObjectDesignerCanvasMediator";
		
		public function ObjectDesignerCanvasMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			this.objectDesigner.addEventListener( FlexEvent.CREATION_COMPLETE, handleViewCreationComplete);
		}
		
		override public function getMediatorName():String
		{
			return NAME;
		}
		
		override public function listNotificationInterests():Array
		{
			return [];
		}
		
		override public function handleNotification(notification:INotification):void
		{
		}
		
		override public function onRegister():void
		{
			objectDesigner.addEventListener(FlexEvent.CREATION_COMPLETE, handleViewCreationComplete);
		}
		
		private function handleViewCreationComplete( event:FlexEvent ) : void
		{
			objectDesigner.addPropertyTrigger.addEventListener(MouseEvent.CLICK, onAddPropertyTriggerFire);
			objectDesigner.addClassTrigger.addEventListener(MouseEvent.CLICK, onAddClassTriggerFire);
			objectDesigner.label ="New Object";
			objectDesigner.setStyle('backgroundColor', '#494848');
			objectDesigner.x=50;
			objectDesigner.y=50; 
			objectDesigner.percentWidth=100; 
			objectDesigner.percentHeight=100;
		}
		
		private function onAddPropertyTriggerFire( event:MouseEvent ) : void
		{
			
			var classProp:ClassProp = new ClassProp();
			
			if (objectDesigner.selectedObject && objectDesigner.selectedObject.isClass == true) 
			{
				classProp.parentObject = objectDesigner.selectedObject;
				objectDesigner.selectedObject.childNodes.push(classProp);
			}				
	    	classProp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	    	classProp.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	    	
	    	classProp.parentCanvas = this.objectDesigner;
	    	objectDesigner.objectLayer.rawChildren.addChild(classProp);
	    	classProp.width = 200;
	    	classProp.height = 150;
	    	classProp.x = objectDesigner.parent.width/2;
	    	classProp.y = objectDesigner.parent.height/2;
	    	classProp.doubleClickEnabled = true;
	    	classProp.hitArea = classProp.img;
	    	
	    	Tweener.addTween(classProp,{width: 350, height: 175, time: .75, transition: "easeOutElastic"});
		}
				
		public function onMouseDown(event:MouseEvent):void 
		{
				if (objectDesigner.selectedObject != null) {
					objectDesigner.selectedObject.filters = [];
				}
				objectDesigner.selectedObject = event.currentTarget as ObjectCanvas;	
				if (objectDesigner.choosePropParentMode == true && objectDesigner.prevObject != objectDesigner.selectedObject) {
					objectDesigner.prevObject.parentObject = event.currentTarget as ObjectCanvas;
					objectDesigner.selectedObject.childNodes.push(objectDesigner.prevObject);
					objectDesigner.prevObject = null;
					objectDesigner.choosePropParentMode = false;
				}
				else if (objectDesigner.prevObject === objectDesigner.selectedObject) 
				{
					objectDesigner.prevObject.parentObject = null;
					objectDesigner.choosePropParentMode = false;
				}			
				objectDesigner.selectionGlow.play([objectDesigner.selectedObject]);		
				var symbol:ObjectCanvas = event.currentTarget as ObjectCanvas;
				symbol.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
						
		}
		
		private function choosePropParent(event:MouseEvent):void 
		{
        	objectDesigner.choosePropParentMode = true;
        	objectDesigner.prevObject = objectDesigner.selectedObject as ObjectCanvas;
        	objectDesigner.prevObject.parentObject = null;
        }

		private function startMove(symbol:Object):void 
		{
			symbol.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}

		private function mouseMoveHandler(event:MouseEvent):void 
        {                
            var symbol:Object = event.currentTarget as ObjectCanvas;
            if (event.buttonDown == true) 
            {
            	symbol.startDrag();
            }
            else 
            {
            	symbol.stopDrag();
            }
        }
	        
        public function onMouseUp(event:MouseEvent):void 
        {
			(event.currentTarget as ObjectCanvas ).stopDrag();
		}		
		
		private function onAddClassTriggerFire( event:MouseEvent ) : void
		{
			sendNotification( ApplicationFacade.ADD_OBJECT_DESIGNER_CANVAS );
		}
		
		override public function onRemove():void
		{
		}
		
		public function get objectDesigner() : ObjectDesignerCanvas
		{
			return viewComponent as ObjectDesignerCanvas;
		}
		
	}
}